package com.atguigu.bigdata.scala.mytest.chapter07

object Test_WordCount5 {
  def main(args: Array[String]): Unit = {
    //不同省份的商品点击排行
    val list = List(
      ("zhangsan", "河北", "鞋"),
      ("lisi", "河北", "衣服"),
      ("wangwu", "河北", "鞋"),
      ("zhangsan", "河南", "鞋"),
      ("lisi", "河南", "衣服"),
      ("wangwu", "河南", "鞋"),
      ("zhangsan", "河南", "鞋"),
      ("lisi", "河北", "衣服"),
      ("wangwu", "河北", "鞋"),
      ("zhangsan", "河北", "鞋"),
      ("lisi", "河北", "衣服"),
      ("wangwu", "河北", "帽子"),
      ("zhangsan", "河南", "鞋"),
      ("lisi", "河南", "衣服"),
      ("wangwu", "河南", "帽子"),
      ("zhangsan", "河南", "鞋"),
      ("lisi", "河北", "衣服"),
      ("wangwu", "河北", "帽子"),
      ("lisi", "河北", "衣服"),
      ("wangwu", "河北", "电脑"),
      ("zhangsan", "河南", "鞋"),
      ("lisi", "河南", "衣服"),
      ("wangwu", "河南", "电脑"),
      ("zhangsan", "河南", "电脑"),
      ("lisi", "河北", "衣服"),
      ("wangwu", "河北", "帽子")
    )

    //map改变数据结构
    val tuples: List[(String, String, Int)] = list.map(w => (w._2,w._3,1))
    println(tuples)

    //根据地区分组
    val stringToTuples: Map[String, List[(String, String, Int)]] = tuples.groupBy(w => w._1)
    println(stringToTuples)

    val stringToStrings: Map[String, List[String]] = stringToTuples.mapValues(w => w.map(s => (s._2)))
    println(stringToStrings)

    val stringToStringToStrings: Map[String, Map[String, List[String]]] = stringToStrings.mapValues(w => w.groupBy(s => s))
    println(stringToStringToStrings)

    val stringToStringToInt: Map[String, Map[String, Int]] = stringToStringToStrings.mapValues(w => w.mapValues(s => s.size))
    println(stringToStringToInt)

    val stringToTuples1: Map[String, List[(String, Int)]] = stringToStringToInt.mapValues(w => w.toList.sortBy(s => s._2)(Ordering.Int.reverse))
    println(stringToTuples1)

  }
}
